﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using System.Diagnostics.Contracts;

namespace System.Linq.Expressions
{
	/// <summary>
	/// Remove a primeira expressão lambda que for encontrada
	/// </summary>
	internal sealed class LambdaExpressionReducer : ExpressionVisitor
	{
		protected override Expression VisitLambda<T>( Expression<T> node )
		{
			return base.Visit( node.Body );
			//var newNode = ( Expression<T> )base.Visit( node.Body );
			//return node.Body;
		}

		protected override Expression VisitGoto( GotoExpression node )
		{
			return node.Value;	
		}
	}

	public static class LambdaExpressionReducerExtensions
	{
		public static Expression RemoveLambda( this LambdaExpression lambda )
		{
			Contract.Requires<ArgumentNullException>( lambda != null, "lambda" );

			LambdaExpressionReducer reducer = new LambdaExpressionReducer();
			return reducer.Visit( lambda );
		}
	}
}
